/**
 * Copyright (C) 2017 - 2020 https://github.com/joewee
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bifrost.repository;

import com.bifrost.entity.CountryArea;
import com.bifrost.entity.MerchantStore;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * @author joewee
 * @version 1.0.0
 * @Date 2017/11/1 11:40
 */
public interface MerchantStoreRepository extends JpaRepository<MerchantStore, Long>,JpaSpecificationExecutor<MerchantStore>{
    /**
     * 获取所有门店默认门店除外
     *
     * @return
     */
    @Override
    @Query(value = "SELECT * FROM MERCHANT_STORE M WHERE M.IS_AVAILABLE <>0 ", nativeQuery = true)
    List<MerchantStore> findAll();

    /**
     * 查询某区域所有门店
     *
     * @param area
     * @return
     */
    // @Query("select m from MerchantStore m where  m.storeDistrict =?1 and (m.isDefault is null or  m.isDefault = false)")
    List<MerchantStore> findAllByStoreDistrict(CountryArea area);

    /**
     * 查询默认门店
     * @return
     */
    //@Query("select * from MERCHANT_STORE m where m.is_default = 1  limit 1")
    MerchantStore findFirstByIsDefault(Boolean isDefault);

    MerchantStore findFirstByExternalId(String id);

    /**
     * 后台批量删除
     * @param merchantIds
     */
    @Transactional
    @Modifying
    @Query(value = "DELETE FROM MerchantStore  m WHERE merchantId IN (?1)")
    void deleteByMerchantId( Collection merchantIds);

    @Query("SELECT m FROM MerchantStore m WHERE m.storeName LIKE ?1")
    List<MerchantStore> findByStoreName(String name);

    @Query(value = "SELECT * FROM MERCHANT_STORE" , nativeQuery = true)
    List<MerchantStore> getAll();
}
